<?php

/**
 * 系统管理
 */
class SystemAction extends CommonAction {

    /**
     * 系统信息
     */
    public function webInfo() {
        $this->display();
    }

    /**
     * 网站配置
     */
    public function webConfig() {
        $WebConfig = M('Webconfig');
        if ($_POST) {
            foreach ($_POST as $key => $value) {
                if ($value || $value === '0') {
                	$condition = array('name' => $key);
                	if ($WebConfig->where($condition)->count() == 0)
                		$WebConfig->add(array('name' => $key, 'value' => ''));
                    $WebConfig->where($condition)->save(array('value' => $value));
                }
            }
            $this->assign('notice', array('status' => 'success', 'message' => '操作成功！'));
        }
        $this->assign('webConfig', $WebConfig->getField('name, value'));
        $this->display();
    }

    /**
     * 系统配置
     */
    public function sysConfig() {
        $SysConfig = M('Sysconfig');
        if ($_POST) {
            parent::getSysConfig();
            $config = session('sysConfig');
            foreach ($_POST as $key => $value) {
                if ($value) {
                    if ($_FILES && $_FILES[$key]['error']['waterUploadImages'] != 4) {
                        $uploadFile = array(
                            'name' => $_FILES[$key]['name']['waterUploadImages'],
                            'type' => $_FILES[$key]['type']['waterUploadImages'],
                            'tmp_name' => $_FILES[$key]['tmp_name']['waterUploadImages'],
                            'error' => $_FILES[$key]['error']['waterUploadImages'],
                            'size' => $_FILES[$key]['size']['waterUploadImages']
                        );
                        $_FILES[$key] = $uploadFile;
                        $attachConfig = $config['attach'];
                        import('ORG.Net.UploadFile');
                        $upload = new UploadFile();
                        $upload->maxSize = $attachConfig['uploadMaxSize'];
                        $upload->allowExts = explode('|', $attachConfig['imageType']);
                        $upload->saveRule = 'water';
                        $upload->uploadReplace = true;
                        $upload->savePath = './Uploads/waterMark/';
                        if ($upload->upload()) {
                            $uploadInfo = $upload->getUploadFileInfo();
                            $value['waterUploadImages'] = $uploadInfo[0]['savename'];
                        }
                    } else {
                        $value['waterUploadImages'] = $config['water']['waterUploadImages'];
                    }
                    if ($SysConfig->where(array('name' => $key))->count() == 0)
                    	$SysConfig->add(array('name' => $key, 'value' => ''));
                    $SysConfig->where(array('name' => $key))->save(array('value' => serialize($value)));
                    //修改之后删除session中的sysConfig
                    session('sysConfig', null);
                }
            }
            $this->assign('notice', array('status' => 'success', 'message' => '操作成功！'));
            $this->assign('name', $key);
        }
        $data = $SysConfig->getField('name, value');
        $this->assign('sysConfig', array_map('unserialize', $data));
        $this->display();
    }

    /**
     * 清除缓存
     */
    public function clearCache() {
        $this->display();
    }
    
    /**
     * 修改密码
     */
    public function changePassword() {
        $this->display();
    }

}